package com.zb.zbtbackend.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.zb.zbtbackend.feignclient.NewsFeignClient;
import com.zb.zbtbackend.util.JsonUtil;
import com.zb.zbtbackend.util.UuidUtil;
import com.zb.zbtbackend.vo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by Administrator on 2018/12/15.
 */
@Controller
@RequestMapping("/zbt/zbtbackend/news")
public class NewsController {
    @Autowired
    private NewsFeignClient newsFeignClient;

    @RequestMapping("/saveNews")
    public String addNews(HttpSession session,News news){
        String json = newsFeignClient.saveNews(news);
        news.setNewsid(UuidUtil.get32UUID());
        news.setCreateby(2);
        news.setCreatedate(new Date());

        JsonNode node = JsonUtil.jsonNode(json);
        String target = node.get("target").toString();
        String result = "";
        if("保存成功".equals(target)){
            result = "";
        }else{
            result = "";
        }
        return result;
    }

    @RequestMapping("/updateNews")
    public String updateNews(HttpSession session,News news){
        String id = news.getNewsid();
        String oleJson = newsFeignClient.findNewsById(id);
        String resultJson = newsFeignClient.updateNews(news);
        JsonNode node = JsonUtil.jsonNode(resultJson);
        int code = Integer.parseInt(node.get("code").toString());
        String result = "";
        if(code == 200){
            result = "";
        }else{
            result = "";
        }
        return result;
    }

    @RequestMapping("/deleNews")
    public String deleNews(String newsid,Model model){
        String json = newsFeignClient.deleNews(newsid);
        JsonNode node = JsonUtil.jsonNode(json);

        int code = Integer.parseInt(node.get("code").toString());

        if(code == 200){
            model.addAttribute("deleteFlag","ok");
        }else{
            model.addAttribute("deleteFlag","error");
        }
        return "";
    }

    @RequestMapping("/findNewsList")
    @ResponseBody
    public Map<String,Object> findNewsList(String newstypeid, String currentPage){
        String newsJson=newsFeignClient.findNewsList(newstypeid,currentPage);
        JsonNode newsNode = JsonUtil.jsonNode(newsJson);
        JsonNode newslistJson = newsNode.get("target").get("newslist");
        Iterator<JsonNode> newsEle = newslistJson.elements();
        List<News> newsList=new ArrayList<>();
        while (newsEle.hasNext()){
            JsonNode obj = newsEle.next();
            News n = JSONObject.parseObject(obj.toString(), News.class);
            newsList.add(n);
        }
        Map<String,Object> result=new HashMap<>();
        result.put("totalPage",newsNode.get("target").get("totalPage").toString());
        result.put("count",newsNode.get("target").get("count").toString());
        result.put("newsList",newsList);
        return result;
    }
}
